package com.padakeji.android.tumou.model;

import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.loopj.android.http.BaseJsonHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.padakeji.android.tumou.R;
import com.padakeji.android.tumou.bean.News;
import com.padakeji.android.tumou.util.AsyncHttpClientWrapper;
import com.padakeji.android.tumou.util.TuMouUIHelper;

import org.apache.http.Header;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by AtanL on 2015/9/8.
 */
public class NewsModel {
    public interface OnNewsListListener {
        void onNewsList(List<News> news);
    }

    public interface OnNewsCreateListener {
        void onNewsCreate();
    }

    private OnNewsListListener onNewsListener;
    private OnNewsCreateListener onNewsCreateListener;

    public void setOnNewsListener(OnNewsListListener l) {
        this.onNewsListener = l;
    }

    public void setOnNewsCreateListener(OnNewsCreateListener l) {
        this.onNewsCreateListener = l;
    }

    public void loadNews(double lat, double lng) {

        RequestParams params = new RequestParams();
        params.put("lat", lat);
        params.put("lng", lng);

        AsyncHttpClientWrapper.getInstance().get(R.string.service_news_list, null, new BaseJsonHttpResponseHandler<JSONObject>() {
            @Override
            public void onSuccess(int statusCode, Header[] headers, String rawJsonResponse, JSONObject response) {
                TuMouUIHelper.getInstance().showToast(statusCode + " " + response.toString(), Toast.LENGTH_SHORT);
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, Throwable throwable, String rawJsonData, JSONObject errorResponse) {
                TuMouUIHelper.getInstance().showToast(statusCode + " " + rawJsonData, Toast.LENGTH_SHORT);
            }

            @Override
            protected JSONObject parseResponse(String rawJsonData, boolean isFailure) throws Throwable {
                if (!isFailure) {
                    try {
                        return JSONObject.parseObject(rawJsonData);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
                return null;
            }
        }, 1);

        List<News> news = new ArrayList<>();
        if (onNewsListener != null) {
            onNewsListener.onNewsList(news);
        }
    }


    public void createNews(News news) {
        AsyncHttpClientWrapper.getInstance().post(R.string.service_news_create, null, JSONObject.parseObject(JSON.toJSONString(news)), new BaseJsonHttpResponseHandler<JSONObject>() {
            @Override
            public void onSuccess(int statusCode, Header[] headers, String rawJsonResponse, JSONObject response) {
                TuMouUIHelper.getInstance().showToast("" + response.toJSONString(), Toast.LENGTH_SHORT);
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, Throwable throwable, String rawJsonData, JSONObject errorResponse) {
                TuMouUIHelper.getInstance().showToast("" + rawJsonData, Toast.LENGTH_SHORT);
            }

            @Override
            protected JSONObject parseResponse(String rawJsonData, boolean isFailure) throws Throwable {
                if (!isFailure) {
                    try {
                        return JSONObject.parseObject(rawJsonData);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
                return null;
            }
        });
    }

}
